<?php
class m_tahun_ajaran extends Model{
    var $table = "tahun_ajaran";
    
    function getCountRows(){
        $query = $this->db->query("select ta.*,s.nama as semester,s.id as id_semester from tahun_ajaran ta join semester s on ta.id_semester = s.id");
        
        return $query->num_rows();
    }
    function getListTahunAjaran($offset = NULL,$limit = NULL){
        $where = "";
        if($offset != NULL || $limit != NULL){
            $where .= "limit $offset,$limit";
        }
        $query = $this->db->query("select ta.*,s.nama as semester,s.id as id_semester from tahun_ajaran ta join semester s on ta.id_semester = s.id order by ta.id desc $where");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getActiveTahunAjaran(){
        $query = $this->db->query("select ta.*,s.nama as semester,s.id as id_semester from tahun_ajaran ta join semester s on ta.id_semester = s.id where ta.status = '1'");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function addTahunAjaran($data){
        $this->db->insert($this->table,$data);
        $id = $this->db->insert_id();
        
        $update = $this->db->query("update tahun_ajaran set status='0' where id != $id");
        if($update){
            return TRUE;
        }else return FALSE;
    }
    function getTahunAjaranById($id){
        $query = $this->db->query("select ta.*,s.nama as semester,s.id as id_semester from tahun_ajaran ta join semester s on ta.id_semester = s.id where ta.id = '$id'");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function editTahunAjaran($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if($update){
            return TRUE;
        }else return FALSE;
    }
    function deleteTahunAjaran($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if($delete){
            return TRUE;
        }else return FALSE;
    }
    function aktivasiTahunAjaran($data){
        $this->db->where('id',$data['id']);
        $aktivasi = $this->db->update($this->table,$data);
        $status = ($data['status'] == 1)?0:1;
        $update = $this->db->query("update tahun_ajaran set status = '$status' where id != $data[id]");
        if($update){
            return true;
        }else return false;
    }
}
?>
